package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.FeedCommentBean;
import com.voute.Model.Bean.FeedCommentData;
import com.voute.Model.Bean.FeedCommentDataEntity;
import com.voute.Model.Bean.FeedDetailInfoBean;
import com.voute.Model.Bean.VoteResultBean;
import com.voute.Model.ICircleModel;
import com.voute.Model.IFeedDetailInfoModel;
import com.voute.Model.Impl.CircleModelImpl;
import com.voute.Model.Impl.FeedDetailInfoModelImpl;
import com.voute.Presenter.IFeedDetailInfoPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IFeedDetailInfoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

/**
 * Created by liucxu on 2017/4/28.
 */

public class FeedDetailInfoPresenterImpl implements IFeedDetailInfoPresenter {

    private IFeedDetailInfoView feed_view;
    private IFeedDetailInfoModel model;
    private ICircleModel circleModel;

    public FeedDetailInfoPresenterImpl(IFeedDetailInfoView view) {
        feed_view = view;
        model = new FeedDetailInfoModelImpl();
        circleModel = new CircleModelImpl();
    }

    @Override
    public void queryFeedDetailInfo(String feed_hash) {

        model.queryFeedDetailInfo(feed_hash).compose(TransformUtils.<FeedDetailInfoBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                feed_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<FeedDetailInfoBean>() {
            @Override
            public void onSuccess(FeedDetailInfoBean response) {
                feed_view.hideLoading();
                feed_view.setFeedDetailInfo(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.hideLoading();
                feed_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void voteFeed(String hash, String count, String side) {
        HashMap<String, String> params = new HashMap<>();
        params.put("count", count);
        if ("0".equals(side)) {
            side = "right";
        } else {
            side = "left";
        }
        params.put("side", side);
        model.voteFeed(hash, params).compose(TransformUtils.<VoteResultBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                feed_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<VoteResultBean>() {
            @Override
            public void onSuccess(VoteResultBean response) {
                feed_view.hideLoading();
                feed_view.voteSuccess(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.hideLoading();
                feed_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void joinCircle(String hash_circle) {
        circleModel.JoinCircle(hash_circle).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                feed_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                feed_view.hideLoading();
                feed_view.joinCircleSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.hideLoading();
                feed_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void exitCircle(String hash_circle) {
        circleModel.ExitCircle(hash_circle).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                feed_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                feed_view.hideLoading();
                feed_view.exitCircleSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.hideLoading();
            }
        });
    }

    @Override
    public void requestComments(final String hash_feed, final String page) {
        model.queryFeedDetailInfo(hash_feed).flatMap(new Func1<FeedDetailInfoBean, Observable<FeedCommentBean>>() {
            @Override
            public Observable<FeedCommentBean> call(FeedDetailInfoBean feedDetailInfoBean) {
                if (feedDetailInfoBean.getData().isVoted()) {
                    return model.queryMoreComments(hash_feed, page);
                } else {
                    FeedCommentBean return_value = new FeedCommentBean();
                    return_value.setStatus(0);
                    FeedCommentData data = new FeedCommentData();
                    List<FeedCommentDataEntity> comments = new ArrayList<FeedCommentDataEntity>();
                    data.setComments(comments);
                    return_value.setData(data);
                    return Observable.just(return_value);
                }
            }
        }).compose(TransformUtils.<FeedCommentBean>defaultSchedulers()).subscribe(new CommonSubscriber<FeedCommentBean>() {
            @Override
            public void onSuccess(FeedCommentBean response) {
                feed_view.setMoreComments(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void upVote(String cid) {
        model.upVote(cid).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {

            }

            @Override
            public void onFailure(BaseBean response) {

            }
        });
    }

    @Override
    public void reportFeed(String hash, String content) {
        model.reportFeed(hash, content).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                feed_view.reportFeedSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                feed_view.showError(response.getMsg());
            }
        });
    }
}
